//package vip.mtdp.usercenter.controller;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.core.MethodParameter;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//import org.springframework.http.server.ServerHttpRequest;
//import org.springframework.http.server.ServerHttpResponse;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.bind.annotation.RestControllerAdvice;
//import org.springframework.web.servlet.NoHandlerFoundException;
//import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
//import vip.mtdp.common.exception.AppRTException;
//import vip.mtdp.common.response.CommonResponse;
//
//import javax.servlet.http.HttpServletRequest;
//
///** 2022-06-26 19:00:12 迁移到cloud-dubbo-starter 模块
// * @author wangguoqing
// * @since 2020/5/1 22:10
// **/
//
//@Slf4j
//@RestController
//@RestControllerAdvice
//public class UserAdviceController implements ResponseBodyAdvice {
//    @Override
//    public boolean supports(MethodParameter methodParameter, Class aClass) {
//        return true;
//    }
//
//    @Override
//    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
//        if(o instanceof CommonResponse){
//            return o;
//        }
//        return builderCommonResponse(String.valueOf(HttpStatus.OK.value()),HttpStatus.OK.getReasonPhrase(),o);
//    }
//
//    /**
//     * 异常处理
//     * @param ex
//     * @param request
//     * @return
//     */
//    @ExceptionHandler(Exception.class)
//    public CommonResponse ExceptionHandler(Exception ex, HttpServletRequest request){
//        log.info("=====> ex handler = {}",ex);
//        //默认 返回 500
//        CommonResponse commonResponse = new CommonResponse(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),null);
//        //app 业务异常,code,message 原样返回
//        if(ex instanceof AppRTException){
//            AppRTException appEx = (AppRTException) ex;
//            commonResponse.setCode(appEx.getCode());
//            commonResponse.setMessage(appEx.getMessage());
//        }
//        //not found 404
//        if(ex instanceof NoHandlerFoundException){
//            commonResponse = builderCommonResponse(String.valueOf(HttpStatus.NOT_FOUND.value()),HttpStatus.NOT_FOUND.getReasonPhrase(),null);
//        }
//        return commonResponse;
//    }
//
//    /**
//     * 组装返回对象
//     * @param code
//     * @param message
//     * @param data
//     * @return
//     */
//    private CommonResponse builderCommonResponse(String code, String message, Object data){
//        CommonResponse commonResponse = new CommonResponse(code,message,data);
//        return commonResponse;
//    }
//}
